<?php

namespace Frieza\Modules\Position\Controllers;

use Biz\service\advertisement\apiModule\CardWithPos;
use Doraemon\service\position\PositionConstInterface;
use Doraemon\tools\BlockType;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Form;
use Exception;
use Illuminate\Http\Request;


class CustomCardController extends BaseController
{
    use BaseTrait;

    public $base_field_ext_type = CardWithPos::CARD_EXT_TYPE_BANNER;

    /**
     * @throws Exception
     */
    public function saveModule(Request $request, $baseInfoId)
    {
        $pos = $request->input('base.pos', 0);

//            if ($pos > 10)  throw new Exception('展示位置必选小于等于10');
//            $link = $request->input('base.link', []);
//            if (empty($link['link_params']['id'])) throw new Exception('noteID为必填项');
        $this->posCheck($pos);

        $ext_type = $request->input("base.ext_type", "");
        if ($ext_type == CardWithPos::CARD_EXT_TYPE_BANNER) {
            $rules = $request->input('rules', []);
            $pic_id = $request->input("base.pic_id", "");
            if (empty($pic_id)) throw new Exception('图片必须上传');
            $img_size = $rules['img_size'] ?? 0;
            //修改或者复制,并且图片没有被编辑过,不进行校验
            if ($img_size != 0) $this->formRulesValid($rules, 0);
        }
    }

    public function extendList(Grid $grid)
    {
        $grid->column('pos', '展示位置')->label($this->labelColor());
        $grid->column('card_info', '卡片信息')->display(function ($title, Grid\Column $column) {
            $display = [CardWithPos::CARD_EXT_MAP[$this->ext_type] ?? CardWithPos::CARD_EXT_MAP[CardWithPos::CARD_EXT_TYPE_BANNER]];
            if ($this->ext_type == CardWithPos::CARD_EXT_TYPE_CONTENT) {
                $display[] = "内容类型:" . BlockType::name($this->obj_type);
                $display[] = "内容id:" . $this->obj_id;
            } else {
                $images[] = "<img src='{$this->pic_url}' style=\"max-width:200px;max-height:200px\" class=\"img img-thumbnail\"/>";
                $display[] = implode('<br/><br/>', $images);
            }

            return implode('<br/>', $display);
        });
    }


    public function beforeTitleForm(Form $form)
    {
        $card_pos = $this->getPositionConfOptions('card_pos', '');
        //处理 card_pos
        if ($card_pos) {
            $posOptions = $this->generatePosPosition();
            $posForm = $form->radioButton('base.pos', '展示位置')->options($posOptions)->required();
            if ($this->_posHelp) $posForm->help($this->_posHelp);
        }
    }

    public function beforeLinkForm(Form $form)
    {
        $typeOptions = CardWithPos::CARD_EXT_MAP;
        if (empty($this->getPositionConfOptions('card_banner'))) {
            unset($typeOptions[CardWithPos::CARD_EXT_TYPE_BANNER]);
        }
        $form->radio('base.ext_type', '卡片类型')
            ->options($typeOptions)
            ->when(CardWithPos::CARD_EXT_TYPE_BANNER, function (Form $form) {
                $this->bannerForm($form);
                $form->multiLinkSelect('base.link', 'Banner跳转链接', $this->bannerAllowLink);
            })->when(CardWithPos::CARD_EXT_TYPE_CONTENT, function (Form $form) {
                $form->multiLinkSelect('base.link', '内容', $this->cardAllowLink);
            });
    }


    public function defaultBaseInfo(Request $request): array
    {
        if ($this->getPositionConfOptions('card_banner', '0') == 0) {
            $this->base_field_ext_type = CardWithPos::CARD_EXT_TYPE_CONTENT;
        }
        $response = parent::defaultBaseInfo($request);
        $response['pos'] = $this->base_field_pos ?: 1;
        //自定义默认链接类型
        if (count($this->cardAllowLink) == 1) {
            $response['link']['link_type'] = current($this->cardAllowLink);
        }
        return $response;
    }
}

